package com.runen.rxnetlibrary.utils;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间格式工具类
 *
 * @author summer
 */
public class TimeUtil {
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    private static SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    private static Calendar mCalendar = Calendar.getInstance();

    /**
     * 暂时格式化时间不调这个方法
     * 以秒的单位计算 个性时间显示 服务器端由于没有存储毫秒数
     *
     * @param timestamp 以秒为单位
     * @return
     */
    public static String converTime(long timestamp) {
        long currentSeconds = System.currentTimeMillis() / 1000L;
        // 与现在时间相差秒数
        long timeGap = currentSeconds - timestamp;
        String timeStr;
        // 1年以上
        if (timeGap > 24 * 60 * 60 * 30 * 365) {
            timeStr = timeGap / (24 * 60 * 60 * 30 * 365) + "年前";
            // 30天以上
        } else if (timeGap > 24 * 60 * 60 * 30) {
            timeStr = timeGap / (24 * 60 * 60 * 30) + "月前";
            // 1-30天
        } else if (timeGap > 24 * 60 * 60) {
            timeStr = timeGap / (24 * 60 * 60) + "天前";
            // 1小时-24小时
        } else if (timeGap > 60 * 60) {
            timeStr = timeGap / (60 * 60) + "小时前";
            // 1分钟-59分钟
        } else if (timeGap > 60) {
            timeStr = timeGap / 60 + "分钟前";
        } else {// 1秒钟-59秒钟
            timeStr = "1分钟前";
        }
        return timeStr;
    }

    /**
     * 格式化 当前时间
     *
     * @param form 格式字符串 可为空串
     * @return
     */
    public static String getCurrentDateTime(String form) {
        String localform = form;
        if ("".equals(localform)) {
            localform = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat time = new SimpleDateFormat(localform);
        return time.format(new Date());
    }

    /**
     * 格式化 当前时间
     *
     * @param form 格式字符串 可为空串
     * @return
     */
    public static long getCurrentDateTimeToLong(String form) {
        long time = 0;
        try {
            String localform = form;
            if ("".equals(localform)) {
                localform = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat(localform);
            String timeStr = dateFormat.format(System.currentTimeMillis());
            Date date = dateFormat.parse(timeStr);
            time = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 格式化 时间
     *
     * @param form 格式字符串 可为空串
     * @return
     */
    public static long getDateTimeToLong(String form) {
        long time = 0;
        try {
            String localform = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat dateFormat = new SimpleDateFormat(localform);
            if (!TextUtils.isEmpty(form)) {
                Date date = dateFormat.parse(form);
                time = date.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    public static String dealDateFormat2(String oldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            DateFormat df;
            if (oldDate.contains("T")) {
                df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                Date date = df.parse(oldDate);
                SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
                date1 = df1.parse(date.toString());
            } else {
                SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date1 = time.parse(oldDate);
            }
            df2 = new SimpleDateFormat("yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String time = df2.format(date1);
        time = time.replace("-", ".");
        return time;
    }


    public static String dealDateFormat(String oldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            DateFormat df;
            if (oldDate.contains("T")) {
                df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                Date date = df.parse(oldDate);
                SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
                date1 = df1.parse(date.toString());
            } else {
                SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date1 = time.parse(oldDate);
            }
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String time = df2.format(date1);
        time = time.replace("-", ".");
        return time;
    }
    public static String dealDateFormat1(String oldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            DateFormat df;
            if (oldDate.contains("T")) {
                df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                Date date = df.parse(oldDate);
                SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
                date1 = df1.parse(date.toString());
            } else {
                SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date1 = time.parse(oldDate);
            }
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String time = df2.format(date1);
        return time;
    }
    /**
     * @param dt   构造Date
     * @param form 格式化字符串 可为空串
     * @return
     */
    public static String dateTime2String(Date dt, String form) {
        return dateTime2String(dt.getTime(), form);
    }

    public static String dateTime2String(long dt, String form) {
        String localform = form;
        if ("".equals(localform)) {
            localform = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat time = new SimpleDateFormat(localform);
        return time.format(dt);
    }

    /**
     * 时间戳转换格式化的时间
     *
     * @param ldt  传入值要是毫秒精度
     *             时间戳
     * @param form yyyy-MM-dd HH:mm:ss 格式化字符串 可为空
     * @return
     */
    public static String getTime2String(long ldt, String form) {
        String localform = form;
        if ("".equals(localform)) {
            localform = "yyyy-MM-dd HH:mm:ss";
        }
        Date dt = new Date(ldt);
        SimpleDateFormat time = new SimpleDateFormat(localform);
        return time.format(dt);
    }

    /**
     * 以字串形式的时间进行计算，增加或减少iHouer小时，返回日期时间字串
     *
     * @param dateString 必须为这样的格式yyyy-MM-dd HH:mm:ss
     * @param hour
     * @return
     */
    public static String stringDateTimePlus(String dateString, int hour) {
        DateFormat dateFormat;
        // 设定格式
        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        try {
            // util类型
            Date timeDate = dateFormat.parse(dateString);
            // Timestamp类型,timeDate.getTime()返回一个long型
            java.sql.Timestamp dateTime = new java.sql.Timestamp(
                    timeDate.getTime() + (hour * 60 * 60 * 1000L));
            return dateTime.toString().substring(0, 19);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateString;
    }

    /**
     * 当前时间戳加减
     *
     * @param pattern
     * @param hour    以小时为单位
     * @return
     */
    public static String timestampPlus(String pattern, int hour) {
        return getTime2String(System.currentTimeMillis() + hour * 60 * 60 * 1000L, pattern);
    }

    /**
     * 字符串解析成Date对象
     *
     * @param dataString 要解析字符串
     * @param pattern    要解析的格式 如 yyyy年MM月dd
     * @return Date
     */
    public static Date parseStringToDate(String dataString, String pattern) {
        // 设定格式
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            return dateFormat.parse(dataString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 数字补零操作如:01
     *
     * @param obj
     * @return
     */
    public static String timeZeroFill(Object obj) {
        String parten = "00";
        DecimalFormat decimal = new DecimalFormat(parten);
        return decimal.format(obj);
    }

    /**
     * 该方法时间戳精度到s
     * 5分钟之内，显示“刚刚”；
     * 今天之内，显示“今天 XX：XX”；
     * 昨天之内，显示“昨天 XX：XX”；
     * 昨天之前的时间段，显示具体日期“xx月XX日”；
     *
     * @param timestamp 以秒为单位
     * @return
     */
    public static String converFromSecondTime(long timestamp) {
        long currentSeconds = System.currentTimeMillis();
        // 与现在时间相差秒数
        long timeGap = (currentSeconds - timestamp) / 1000;
        String timeStr;
        // 1年以上
        if (timeGap > 24 * 60 * 60 * 30 * 365) {
            timeStr = timeGap / (24 * 60 * 60 * 30 * 365) + "年前";
            // 30天以上
        } else if (timeGap > 24 * 60 * 60 * 30) {
            timeStr = timeGap / (24 * 60 * 60 * 30) + "月前";
            // 1-30天
        } else if (timeGap > 24 * 60 * 60) {
            timeStr = timeGap / (24 * 60 * 60) + "天前";
            // 1小时-24小时
        } else if (timeGap > 60 * 60) {
            timeStr = timeGap / (60 * 60) + "小时前";
            // 1分钟-59分钟
        } else if (timeGap > 60) {
            timeStr = timeGap / 60 + "分钟前";
        } else {// 1秒钟-59秒钟
            timeStr = "1分钟前";
        }
        return timeStr;
    }

    /**
     * 格式化毫秒
     *
     * @param millisecond
     * @return
     */
    public static String formatMillisecond(long millisecond) {
        int hour, minute, second;
        millisecond = millisecond / 1000;
        second = (int) (millisecond % 60);
        millisecond = millisecond / 60;
        minute = (int) (millisecond % 60);
        millisecond = millisecond / 60;
        hour = (int) (millisecond % 60);
        return String.format(Locale.getDefault(), "%02d:%02d:%02d", hour, minute, second);
    }

    /**
     * 时间转换天时分秒
     *
     * @param ms
     * @return
     */
    public static String formatMillisecondData(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        if (day != 0 && hour != 0 && minute != 0) {
            String dayInfo = "%d 天 %02d : %02d : %02d";
            return String.format(Locale.getDefault(), dayInfo, day, hour, minute, second);
        } else if (day == 0 && hour != 0 && minute != 0) {
            return String.format(Locale.getDefault(), "%02d : %02d : %02d", hour, minute, second);
        } else if (day == 0 && hour == 0 && minute != 0) {
            return String.format(Locale.getDefault(), "%02d : %02d", minute, second);
        } else if (day == 0 && hour == 0 && minute == 0) {
            return String.format(Locale.getDefault(), "%02d", second);
        } else {
            String dayInfo = "%d 天 %02d : %02d : %02d";
            return String.format(Locale.getDefault(), dayInfo, day, hour, minute, second);
        }
    }

    /**
     * 格式化毫秒配速
     *
     * @param millisecond
     * @return
     */
    public static String formatMillisecondMinkm(long millisecond) {
        int hour, minute, second;
        millisecond = millisecond / 1000;
        second = (int) (millisecond % 60);
        millisecond = millisecond / 60;
        minute = (int) (millisecond % 60);
        millisecond = millisecond / 60;
        hour = (int) (millisecond % 60);
        return String.format(Locale.getDefault(), "%02d'%02d''", minute, second);
    }

    /**
     * 获取当前的日期
     *
     * @return yyyy年MM月dd日
     */
    public static String getCurrentDate() {
        String currentDateStr = dateFormat1.format(mCalendar.getTime());
        return currentDateStr;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    public static String getMonth(String month) {
        switch (month) {
            case "01":
                return "一月";
            case "02":
                return "二月";
            case "03":
                return "三月";
            case "04":
                return "四月";
            case "05":
                return "五月";
            case "06":
                return "六月";
            case "07":
                return "七月";
            case "08":
                return "八月";
            case "09":
                return "九月";
            case "10":
                return "十月";
            case "11":
                return "十一月";
            case "12":
                return "十二月";
        }
        return "";
    }

    /*
     * 毫秒转化Day
     */
    public static String formatDay(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;
        long day = ms / dd;
        String dayInfo = "%d";
        return String.format(dayInfo, day);

    }

    /*
     * 毫秒转化Day
     */
    public static String formatHour(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;
        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        String dayInfo = "%02d";
        return String.format(dayInfo, hour);
    }

    /*
     * 毫秒转化Day
     */
    public static String formatMinute(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;

        String dayInfo = "%02d";
        return String.format(dayInfo, minute);
    }

    /*
     * 毫秒转化Day
     */
    public static String formatSecond(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "时");
        }
        if (minute > 0) {
            sb.append(minute + "分");
        }
        if (second > 0) {
            sb.append(second + "秒");
        }

        return sb.toString();
    }
}